package com.gxa.ssm251.service.impl;


import com.gxa.ssm251.mapper.CommentMapper;
import com.gxa.ssm251.pojo.CmRatingCopyWriting;
import com.gxa.ssm251.pojo.Comment;
import com.gxa.ssm251.service.CommentService;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 舒空
 */
@Controller("commentService")
public class CommentServiceImpl implements CommentService {
    @Resource
    private final CommentMapper commentMapper = null;

    @Override
    public Integer selectAllComment() {
        return commentMapper.selectAllComment();
    }

    @Override
    public List<Comment> selectAllByPage(Integer page, Integer size) {
        List<Comment> comments = commentMapper.selectAllByPage((page - 1) * size, size);
        for (Comment comment : comments) {
            List<String> list = new ArrayList<>();
            List<Integer> integerList = new ArrayList<>();
            List<CmRatingCopyWriting> cmRatingCopyWritingList = comment.getCmRatingCopyWritingList();
            for (CmRatingCopyWriting cmRatingCopyWriting : cmRatingCopyWritingList) {
                String cmrCwValue = cmRatingCopyWriting.getCmrCwValue();
                Integer cmrCwId = cmRatingCopyWriting.getCmrCwId();
                list.add(cmrCwValue);
                integerList.add(cmrCwId);
            }
            comment.setStringList(list);
            comment.setIdList(integerList);
        }
        return comments;
    }

    @Override
    public List<Comment> selectLikeParam(String param, Date startTime, Date endTime) {
        List<Comment> comments = commentMapper.selectLikeParam("%" + param + "%", startTime, endTime);
        for (Comment comment : comments) {
            List<String> list = new ArrayList<>();
            List<Integer> integerList = new ArrayList<>();
            List<CmRatingCopyWriting> cmRatingCopyWritingList = comment.getCmRatingCopyWritingList();
            for (CmRatingCopyWriting cmRatingCopyWriting : cmRatingCopyWritingList) {
                String cmrCwValue = cmRatingCopyWriting.getCmrCwValue();
                Integer cmrCwId = cmRatingCopyWriting.getCmrCwId();
                list.add(cmrCwValue);
                integerList.add(cmrCwId);
            }
            comment.setStringList(list);
            comment.setIdList(integerList);
        }
        return comments;
    }

    @Override
    public Integer updateIsVisible(Boolean isVisible, String id) {
        return commentMapper.updateIsVisible(isVisible, id);
    }

    @Override
    public Integer delete(String id) {
        return commentMapper.delete(id);
    }


}
